package com.controller;


import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xiao zeng on 2017/6/29.
 */
@Controller
@RequestMapping("download")
public class download {

    @RequestMapping("/download")//安全下载
    public ResponseEntity<byte[]> down(HttpServletRequest req) throws IOException {
        String f = req.getServletContext().getRealPath("/upload/comm/") + "timg.jpg";
        File file = new File(f);
        byte[] body = null;
        InputStream is = new FileInputStream(file);
        body = new byte[is.available()];
        is.read(body);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + file.getName());
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;
    }


    @RequestMapping("/down")//登陆后下载
    public ResponseEntity<byte[]> download(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ResponseEntity<byte[]> entity = null;

            String name = req.getParameter("name");//获取文件名
            byte[] body = null;
            String f = req.getServletContext().getRealPath("/upload/comm/") + name;//文件资源所在路径
            File file = new File(f);
            InputStream is = new FileInputStream(file);
            body = new byte[is.available()];
            is.read(body);
            HttpHeaders headers = new HttpHeaders();
            String fn = new SimpleDateFormat("yyyymmddhhmmss ").format(new Date()) + name.substring(name.lastIndexOf("."));//修改下载文件名

            headers.add("Content-Disposition", "attchement;filename=" + fn);
            HttpStatus statusCode = HttpStatus.OK;
            entity = new ResponseEntity<byte[]>(body, headers, statusCode);

        return entity;
    }


    @RequestMapping("/mydown")//web 通用下载
    public void mydown(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String fileName = req.getParameter("name");
        if (fileName == null) {
            resp.setCharacterEncoding("utf-8");
            PrintWriter out = resp.getWriter();
            out.print("没有下载的文件...");
            out.flush();
            out.close();
        } else {
            resp.setContentType("application/force-download");// 设置强制下载不打开

            String fn = new SimpleDateFormat("yyyyMMddhhmmssS").format(new Date()) + fileName.substring(fileName.lastIndexOf("."));
            resp.addHeader("Content-Disposition", "attachment;fileName=" + fn);// 设置下载文件名

            String path = req.getServletContext().getRealPath("/upload/comm/"); //文件下载源路径

            File file = new File(path, fileName);
            byte[] buf = new byte[10240];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = resp.getOutputStream();
                int i = bis.read(buf);
                while (i != -1) {
                    os.write(buf, 0, i);
                    i = bis.read(buf);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

